package com.zzw.baseadapterdemo2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class KobeAdapter extends BaseAdapter {
    private List<Kobe> data;
    private Context context;

    // 构造方法二型注入属性
    public KobeAdapter(Context context, List<Kobe> data){
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size(); // 获取ListView中的Item的个数
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);  // 返回自定义事件对象
    }

    @Override
    public long getItemId(int position) {  // 返回对象的主键值
        return data.get(position).getId();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.activity_kobe,null);
        Kobe k = data.get(position);
        ImageView show = (ImageView)convertView.findViewById(R.id.KOBE);
        show.setBackgroundResource(k.getShow());

        TextView title = (TextView)convertView.findViewById(R.id.tv);
        title.setText(k.getTitle());
        TextView content = (TextView)convertView.findViewById(R.id.content);
        content.setText(k.getContent());

        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,data.get(position).getTitle(),Toast.LENGTH_SHORT).show();
            }
        });

        content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,data.get(position).getContent(),Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }
}
